package com.fly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fly.common.R;
import com.fly.pojo.*;
import com.fly.service.BlogService;
import com.fly.service.TagService;
import com.fly.service.TagUserService;
import com.fly.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TagUserService tagUserService;

    /**
     * 获取blog信息
     * @param request
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/getBlogs")
    public R<Page> getBlog( int page, int pageSize, HttpServletRequest request){
        Long ID =(Long) request.getSession().getAttribute("id");
        Page<Blog> pageInfo =new Page<>(page,pageSize);
        Page<Blogpojo> blogpojoPage =new Page<>();
        Blogpojo blogpojo=new Blogpojo();

        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<Blog>();
        queryWrapper.eq(Blog::getUserId,ID);
        blogService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,blogpojoPage,"records");

        List<Blog> records =pageInfo.getRecords();

        List<Blogpojo> BlogDto = records.stream().map((item)->{
            Blogpojo blogpojo1 =new Blogpojo();
            BeanUtils.copyProperties(item,blogpojo1);

                Long id =item.getTypeId();

            Type type=typeService.getById(id);

            if (type!=null){
                String types=type.getName();
                blogpojo1.setType(types);

            }
            return blogpojo1;
        }).collect(Collectors.toList());

        blogpojoPage.setRecords(BlogDto);


        return R.success(blogpojoPage);
    }

    @PostMapping("/TypeAndTag")
    public R<Typedto> TypeAndTags(){
        Typedto typedto =new Typedto();
        List<Type> type = typeService.list();
        typedto.setTypes(type);
         typedto.setTags(tagService.list());
        return R.success(typedto);
    }
    @PostMapping("/save")
    public  R<String> saveBlog(@RequestBody Blogpojo blog ,HttpServletRequest request){
        HttpSession session=request.getSession();
        Long id = (Long) session.getAttribute("id");
        blogService.saveBlog(blog,id);


        return R.success("保存成功");
    }


    @GetMapping("/{id}")
    public R<Blogpojo> edit(@PathVariable Long id){
        Blogpojo blogpojo=new Blogpojo();

        Blog blog=blogService.getById(id);
        BeanUtils.copyProperties(blog,blogpojo);
        blogpojo.setType(String.valueOf(typeService.getById(blog.getTypeId()).getName()));


        blogpojo.setTagId(tagService.getById(tagUserService.getById(blog.getId()).getTagsId()).getId());
        return R.success(blogpojo);
    }



    @PostMapping("/update")
    public R<String> update(@RequestBody Blogpojo blog,HttpServletRequest request){
        log.info(blog.toString());
        HttpSession session=request.getSession();
        Long id = (Long) session.getAttribute("id");
        System.out.println(blog+"asd");
        blogService.updaBlog(blog,id);

        return R.success("更新成功");
    }

    @DeleteMapping
    public R<String> deleteBolg(@RequestBody Map id ) {
        Integer idd= (Integer) id.get("id");
        blogService.removeById(idd);
        tagUserService.removeById(idd);
        System.out.println(idd);
        return R.success("删除成功");
    }
}
